package com.jky.stmall.front.mapper;

import com.jky.stmall.front.entity.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {

    //添加
    @Insert({"insert into `order`",//反单引号，
            "(id,user_id,addr_id)",
            "value(#{id},#{userId},#{addrId})",
    })
    int insert(Order order);
    @Delete("delete from `order` where if=#{id}")
    //删除
    int delete(String id);

    //更新
    @Update({
            "<script>",
            "update `order`",
            "<set>",
            "<if test='addrId!=null'> addr_id=#{addrId},</if>",
            "<if test='status!=null'> status=#{status},</if>",
            "</set>",
            "where id=#{id}",
            "</script>"
    })
    int update(Order order);
    //根据Id查询
    @Select("select * from `order` where id =#{id}")
    @Results(id = "order",
    value = {
            @Result(column = "addr_id",property = "addrId"),
            @Result(column = "addr_id",property = "addr",
            one=@One(select = "com.situ.stmall.front.mapper.AddrMapper.selectById")),
            @Result(column = "id",property ="id" ,id=true),
            @Result(column = "id",property = "orderDetails",
            many=@Many(select = "com.jky.stmall.front.mapper.OrderDetailMapper.selectByOrderId"))
    })
    Order selectById(String id);
    //根据用户ID查询
    @Select({"select * from `order` where user_id=#{userId}",
            "order by create_time desc"})
    @ResultMap("order")
    List<Order> selectByUserId(Integer userId);
}
